package com.be.beadmin.design.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.design.dto.input.StandProductDto;
import com.be.beadmin.design.dto.output.StandProductEx;
import com.be.beadmin.design.entity.StandProduct;
import com.be.beadmin.design.service.ProductBomOverviewService;
import com.be.beadmin.design.service.StandFileInfoService;
import com.be.beadmin.design.service.StandProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author Jiahao Liu
* @date 2023-02-13
**/
@Api(tags = {"设计管理-标准柜体"})
@RestController
public class StandProductController {

    @Autowired
    private  StandProductService standProductService;

    @Autowired
    private StandFileInfoService standFileInfoService;

    @Autowired
    private ProductBomOverviewService productBomOverviewService;

    private String bucketName = "standfile";

    @ApiOperation("审批标准柜体")
    @PostMapping("/design/approveStandProduct")
    public ResultModel approveStandProduct(
            @ApiParam(value = "标准柜体id",required = true) @RequestParam String standTypeId,
            @ApiParam(value = "审批结果",required = true) @RequestParam Integer state,
            @ApiParam(value = "审批描述",required = false) @RequestParam(required = false) String approveText){
        standProductService.approveStandProduct(standTypeId,state,approveText);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("提交新建的标准柜体[待提交->待审批]")
    @PostMapping("/design/submitStandProduct")
    public ResultModel submitStandProduct(
            @ApiParam(value = "标准柜体id",required = true) @RequestParam String standTypeId){
        List<String> result = standProductService.submitStandProduct(standTypeId);
        return ResultModel.success(result);
    }

    @ApiOperation("标准柜体名称描述判重")
    @PostMapping("/design/getStandProductByStandTypeName")
    public ResultModel getStandProductByStandTypeName(
            @ApiParam(value = "标准柜体描述",required = true) @RequestParam String standTypeName){
        return ResultModel.success(standProductService.getStandProductByStandTypeName(standTypeName));
    }

    @ApiOperation("获取所有图纸类型")
    @PostMapping("/design/getDrawingTypeList")
    public ResultModel getDrawingTypeList(){
        List<String> drawingTypeList = standProductService.getDrawingTypeList();
        return ResultModel.success(drawingTypeList);
    }

//    @ApiOperation("获取所有图纸类型-按字典值置顶排列")
//    @PostMapping("/design/getDrawingTypeListSorted")
//    public ResultModel getDrawingTypeListSorted(){
//        // 字典值为1，则表示该图纸类型为必填项，输出时置顶
//        List<String> drawingTypeList = standProductService.getDrawingTypeListSorted();
//        return ResultModel.success(drawingTypeList);
//    }

    @ApiOperation("获取柜体类型")
    @PostMapping("/design/getProductTypeList")
    public ResultModel getProductTypeList(){
        List<String> productTypeList = standProductService.getProductType();
        return ResultModel.success(productTypeList);
    }

    @ApiOperation("获取标准柜体编码信息")
    @PostMapping("/project/getStandProductCode")
    public ResultModel getStandProductCode(){
        List<String> standProductCodes = standProductService.getStandProductCode();
        return ResultModel.success(standProductCodes);
    }

    @ApiOperation("获取标准柜体分页信息")
    @PostMapping(value = "/design/getStandProductByPage")
    public ResultModel getStandProductByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(standProductService.getStandProductByPage(pageEntity)) ;
    }

    @ApiOperation("获取标准柜体类型及编码和名称信息")
    @PostMapping(value = "/design/getStandProductAndType")
    public ResultModel getStandProductAndType()  {
        return ResultModel.success(standProductService.getStandProductCodeAndName()) ;
    }


    @ApiOperation("获取非标柜体编码")
    @PostMapping(value = "/design/getNoStandProductCode")
    public ResultModel getNoStandProductCode()  {
        return ResultModel.success(standProductService.getNoStandProductCode()) ;
    }

    @ApiOperation("新建标准柜体")
    @PostMapping("/design/addStandProduct")
    public ResultModel addStandProduct2(@ApiParam(value = "标准柜体类型",required = true) @RequestParam String standTypeDesc,
                                        @ApiParam(value = "标准柜体描述",required = true) @RequestParam String standTypeName,
                                       @ApiParam(value = "工艺路径",required = true) @RequestParam String bopCode,
                                       @ApiParam(value = "设计总工时") @RequestParam String standDesignWorkHour,
                                       @ApiParam(value = "生产总工时") @RequestParam String standManufactWorkHour,
                                        @ApiParam(value = "主物料id") @RequestParam String materialInfoId,
                                        @ApiParam(value = "主物料编码") @RequestParam String materialCode,
                                         @ApiParam(value = "主物料名称") @RequestParam String materialName,
                                       @ApiParam(value = "备注") @RequestParam(required = false) String memo
    ) throws Exception {
        StandProduct standProduct = standProductService.addStandProduct2(standTypeDesc,standTypeName,bopCode,standDesignWorkHour,standManufactWorkHour,memo,bucketName,materialInfoId,materialCode,materialName);
        return ResultModel.success(standProduct);
    }

    @ApiOperation("删除标准柜体")
    @PostMapping("/design/delStandProduct")
    public ResultModel delStandProduct(@RequestBody StandProductDto standProductDto){
        standProductService.delStandProduct(standProductDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("编辑标准柜体")
    @PostMapping("/design/updateStandProduct")
    public ResultModel updateStandProduct2(
                                            @ApiParam(value = "标准柜体id",required = true) @RequestParam String standTypeId,
                                            @ApiParam(value = "标准柜体类型",required = true) @RequestParam String standTypeDesc,
                                            @ApiParam(value = "标准柜体编码",required = true) @RequestParam String standTypeCode,
                                            @ApiParam(value = "标准柜体描述",required = true) @RequestParam String standTypeName,
                                            @ApiParam(value = "工艺路径",required = true) @RequestParam String bopCode,
                                            @ApiParam(value = "设计工时") @RequestParam String standDesignWorkHour,
                                            @ApiParam(value = "生产工时") @RequestParam String standManufactWorkHour,
                                            @ApiParam(value = "主物料id") @RequestParam String materialInfoId,
                                            @ApiParam(value = "主物料编码") @RequestParam String materialCode,
                                            @ApiParam(value = "主物料名称") @RequestParam String materialName,
                                            @ApiParam(value = "备注") @RequestParam(required = false) String memo) throws Exception {
        standProductService.updateStandProduct2(standTypeId,standTypeDesc,standTypeCode,standTypeName,bopCode,standDesignWorkHour,standManufactWorkHour,memo,bucketName, materialInfoId, materialCode, materialName);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("导出")
    @PostMapping("/design/exportStandProduct")
    public void exportStandProduct(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<StandProductEx> standProductPage = standProductService.getStandProductByPage(pageEntity);
        List<StandProductEx> standProductExes = standProductPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("StandProduct接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), StandProductEx.class).sheet("sheet1").doWrite(standProductExes);
    }

    @ApiOperation("BOM上传")
    @PostMapping("/design/uploadStandFileInfoBom")
    public ResultModel uploadStandFileInfoBom(@ApiParam(value = "标准柜体id",required = true) @RequestParam String standTypeId,
                                           @ApiParam(value = "标准柜体类型",required = true) @RequestParam(required = true) String standTypeDesc,
                                           @ApiParam(value = "标准柜体编码",required = true) @RequestParam(required = true) String standTypeCode,
                                           @ApiParam(value = "标准柜体描述",required = true) @RequestParam String standTypeName,
                                           @ApiParam(value = "标准柜体附件类型（这里就是填的：BOM）",required = true) @RequestParam String category,
                                           @ApiParam(value = "标准柜体附件上传",required = true) @RequestParam(value="file") MultipartFile[] multipartFiles,
                                           @ApiParam(value = "备注") @RequestParam(required = false) String memo) throws Exception {

        for(MultipartFile multipartFile:multipartFiles) {
            try {
                //判断能否解析
                Boolean flag = productBomOverviewService.checkTemplate(multipartFile);
            } catch (Exception e) {
                return ResultModel.error(-2, "上传的BOM文件无法解析，请下载BOM模板查看");
            }
            List<Object> bomCodeAndVersion = standFileInfoService.uploadStandFileInfoBom(standTypeId, standTypeDesc, standTypeCode, standTypeName, category, multipartFile, memo, bucketName);
            String standBomCode = (String) bomCodeAndVersion.get(0);
            int version = (int) bomCodeAndVersion.get(1);
            productBomOverviewService.addStandBomByTemplate(standTypeCode, standBomCode, version, multipartFile);
        }
        return ResultModel.success("附件上传成功");
    }

    @ApiOperation("图纸上传")
    @PostMapping("/design/uploadStandFileInfoDrawing")
    public ResultModel uploadStandFileInfoDrawing(@ApiParam(value = "标准柜体id",required = true) @RequestParam String standTypeId,
                                           @ApiParam(value = "标准柜体类型",required = true) @RequestParam(required = true) String standTypeDesc,
                                           @ApiParam(value = "标准柜体编码",required = true) @RequestParam(required = true) String standTypeCode,
                                           @ApiParam(value = "标准柜体描述",required = true) @RequestParam String standTypeName,
                                           @ApiParam(value = "标准柜体附件类型（图纸类型）",required = true) @RequestParam String category,
                                           @ApiParam(value = "标准柜体附件上传",required = true) @RequestParam(value="file") MultipartFile[] multipartFiles,
                                           @ApiParam(value = "备注") @RequestParam(required = false) String memo) throws Exception {
        standFileInfoService.uploadStandFileInfoDrawing(standTypeId,standTypeDesc,standTypeCode,standTypeName,category,multipartFiles,memo,bucketName);
        return ResultModel.success("附件上传成功");
    }

}